//
//  NSDictionary+FK.m
//  zhslpt
//
//  Created by Macbook Pro on 2022/11/1.
//  Copyright © 2022 jxwdyf. All rights reserved.
//

#import "NSDictionary+FK.h"

@implementation NSDictionary (FK)

- (NSString *)fk_toQueryString
{
    // 拼接url和参数
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *key in self) {
        NSString *value = self[key];
        // 如果参数中有字典或者数组类型的字符串，需要将他们转成URL能识别的字符串
        if ([value containsString:@"{"] || [value containsString:@"["]) {
            value = [value stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        }
        [array addObject:fkFORMAT(@"%@=%@", key, value)];
    }
    NSString *paramStr = [array componentsJoinedByString:@"&"];
    
    // 对参数的中文、空格或其他特殊字符进行编码
    paramStr = [paramStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
    
    return paramStr;
}

@end
